package com.nervenets.shopping.controller.order.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.hibernate.domain.order.GroupBuyOrder;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class GroupBuyOrderPagingParams extends EntityPagingParams<GroupBuyOrder> {
    @ApiModelProperty(value = "商品ID，不传不筛选")
    private long productId;
    @ApiModelProperty(value = "用户ID，不传不筛选")
    private long userId;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<GroupBuyOrder> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("product").get("id"), productId, productId > 0);
        if (userId > 0) {
            query.distinct(true);
            addEqualsPredicate(root.join("users").get("createBy"), userId, true);
        }
    }
}
